import shutil
import sys
import urllib.request
from bs4 import BeautifulSoup
import os
import urllib

DEBUG=0
FUN_WRITE_TXT=0

ProjectDir = os.path.dirname(os.path.realpath(sys.argv[0]))+'/'
WorkDir=''   # 文件夹命名规则：ProjectDir + R_对比文件名
Url= ""
Filename=''
Headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36'}
Web_Packs = []
Local_Packs=[]
WebMore=[]
LocalMore=[]

# 拉取网站内包列表
def pull_web_packageslist():
    # 获取网站源码
    response = urllib.request.Request(url=Url, headers=Headers)
    html = urllib.request.urlopen(response)
    soup = BeautifulSoup(html,"html.parser")
    # 分析网站内有哪些rpm安装包
    for packlist in soup.find_all('a'):
        name = packlist['href']
        if "rpm" in name:
            if "%2B" in name:
                name = str(name).replace('%2B','+') # 这里是因为链接有些使用 %2B 代替了 ‘+’
            Web_Packs.append(name.lstrip().rstrip())
    # print('网站中现有rpm包 {} 个'.format(len(Web_Packs)))

    if DEBUG:
        print('DEBUG ----> 从网站获取到的包：\n', Web_Packs)
    if FUN_WRITE_TXT:
        write_txt(Web_Packs,'web_src')

def read_file_packageslist():
    with open(Filename, 'r', encoding='UTF-8') as f:
        for name in f.read().split('\n'):
            if 'rpm' in name:
                Local_Packs.append(name.lstrip().rstrip())
    # print('本地数据包 {} 个'.format(len(Local_Packs)))

    if DEBUG:
        print('DEBUG ----> 从 {} 中获取到的包：\n',Local_Packs)
    if FUN_WRITE_TXT:
        write_txt(Local_Packs,"file_src")


def diff_packages():
    global WebMore
    global LocalMore
    WebMore = Web_Packs.copy()
    LocalMore = Local_Packs.copy()
    diff = []
    for name in Web_Packs:
        if name in Local_Packs:
            WebMore.remove(name)
            LocalMore.remove(name)
        else:
            diff.append(name)

    WebMore.append('网站多出 {} 个包'.format(len(WebMore)))
    LocalMore.append('系统内多出 {} 个包'.format(len(LocalMore)))
    write_txt(WebMore,'web_more')
    write_txt(LocalMore,"local_more")



def download_packages():
    if len(WebMore)==1 and len(LocalMore)==1:
        pass
    else:
        while True:
            choose = input('====> 是否需要下载网站多出的安装包    [Y/N] \n')
            if choose == 'y' or choose == 'Y':
                download()
                break
            elif choose == 'n' or choose == 'N':
                break
            else:
                print('Warning！！：选择错误,请重新输入：')
def download():
    DownloadDir = WorkDir + 'download/'
    os.mkdir(DownloadDir)
    count = 0
    opener = urllib.request.build_opener()
    opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
    urllib.request.install_opener(opener)
    with open(WorkDir + 'web_more','r',encoding='UTF-8') as f:
        for name in f.read().split('\n'):
            if 'rpm' in name:
                downloadurl = Url + name.replace('+',"%2B")
                print(' 正在下载: {}'.format(downloadurl))
                urllib.request.urlretrieve(downloadurl, DownloadDir+name)
                count = count + 1
    print('共计下载 {} 个包'.format(count))

def write_txt(packlist, filename): # 用来把列表写入文件中
    with open(WorkDir+filename,'a',encoding='UTF-8') as f:
        for name in packlist:
            f.write(name+'\n')
    # print("写入完成：",WorkDir+filename)


def inputinfo():  # 定义系统的 Url(对比连接） , Filename(对比文件) , WorkDir(本次对比的工作目录)
    # 判断参数是否输入正确，正确输入方式： main.py  a.txt  https://.... 传入参数应该为3
    if len(sys.argv)==3:
        global Url
        global Filename
        global WorkDir
        if str(sys.argv[2])[-1]=='/':
            Url = str(sys.argv[2])
        else:
            Url = str(sys.argv[2]) + '/'
        Filename = os.path.basename(str(sys.argv[1]))
        # URL="https://update.cs2c.com.cn/NS/V10/V10SP1/os/adv/lic/base/aarch64/Packages/"
        # Filename = './base1.text'
        WorkDir=ProjectDir+'R_'+Filename+'/'
    else:
        print('请提供完整参数，使用方法为：命令 + 文件名 + 网站地址\n举例：./diffrpm base1.text  https://update.cs2c.com.cn/NS/V10/V10SP1/os/adv/lic/base/aarch64/Packages/')
        sys.exit(1)

def project_init():
    if os.path.exists(WorkDir):
        shutil.rmtree(WorkDir)
        # print("删除文件夹: ",WorkDir)
    os.mkdir(WorkDir)
    # print("生成工作文件夹： ",WorkDir)

    init_str="\
    ********** 任务信息 **********\n\
    * 含有系统包的文件：{}\n\
    * 对比网站地址： {}\n\
    * 该文件对应工作目录：{}\n\
    ********** 任务创建成功 **********".format(Filename, Url, WorkDir)
    print(init_str)

def print_format():
    format_str = " \
    ########## 任务报告 ########## \n\
    #   对比文件：{} \n\
    #   对比外网源地址： {} \n\
    #   网站中现有rpm包 {} 个      系统内现有rpm包 {} 个\n\
    #   网站多出 {} 个包           系统内多出 {} 个包\n\
    #   网站多出包明细： {}\n\
    #   系统内多出包明细： {}\n\
    #   网站包下载目录： {}\n\
    ########## 任务完成 ##########".format(Filename, Url, len(Web_Packs), len(Local_Packs), len(WebMore)-1, len(LocalMore)-1, WorkDir+'web_more', WorkDir+'local_more', WorkDir + 'download/')

    nothing_str =" \n\
    ########## 任务报告 ##########\n\
    #   对比文件：{} \n\
    #   对比外网源地址： {} \n\
    #   网站中现有rpm包 {} 个      系统内现有rpm包 {} 个\n\
    #   网站多出 {} 个包           系统内多出 {} 个包\n\
    #   任务总结：\n\
    #       系统内rpm包和外网源中rpm包，数量版本均相同，无需更新\n\
    ########## 任务完成 ##########".format(Filename, Url, len(Web_Packs), len(Local_Packs), len(WebMore)-1, len(LocalMore)-1)

    if len(WebMore)==1 and len(LocalMore)==1:
        print(nothing_str)
    else:
        print(format_str)



if __name__ == '__main__':
    inputinfo()
    project_init()
    #
    pull_web_packageslist()
    read_file_packageslist()
    diff_packages()
    print("网站多出 {} 个包           系统内多出 {} 个包".format(len(WebMore)-1, len(LocalMore)-1))
    download_packages()
    print_format()

